package org.example.coupon.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.core.constant.TokenConstants;
import org.example.core.utils.PageUtils;
import org.example.core.utils.R;
import org.example.coupon.dao.SeckillPromotionDao;
import org.example.coupon.entity.SeckillPromotionEntity;
import org.example.coupon.entity.SeckillSessionEntity;
import org.example.coupon.service.SeckillSessionService;
import org.example.security.handler.SecurityHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.coupon.service.SeckillPromotionService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Service("seckillPromotionService")
public class SeckillPromotionServiceImpl implements SeckillPromotionService {

    @Autowired
    private SeckillPromotionDao seckillPromotionDao;

    @Autowired
    private SeckillSessionService seckillSessionService;

    @Override
    public R<?> addSeckillPromotion(SeckillPromotionEntity seckillPromotion) {
        if (seckillPromotion.getEndTime().isBefore(seckillPromotion.getStartTime())){
            return R.error("时间设置不合理");
        }
        seckillPromotion.setCreateTime(LocalDateTime.now());
        long userId = Long.parseLong(SecurityHandler.getData().get(TokenConstants.USER_ID));
        seckillPromotion.setUserId(userId);
        int isOK = seckillPromotionDao.saveSeckillPromotion(seckillPromotion);
        if (isOK > 0){
            return R.ok();
        }
        return R.error();
    }

    // 查看秒杀场次
    @Override
    public PageUtils querySeckillPromotionList(Integer pageNum, Integer pageSize, String title, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        List<SeckillPromotionEntity> entityList = seckillPromotionDao.getSeckillPromotionList(title, status, null);
        return new PageUtils(new PageInfo<>(entityList));
    }

    // 查询单个场次信息
    @Override
    public SeckillPromotionEntity getSeckillPromotionInfo(Long id) {
        return seckillPromotionDao.getSeckillPromotionInfo(id);
    }

    @Override
    public R<?> updateSeckillPromotion(SeckillPromotionEntity seckillPromotion) {
        if (seckillPromotion.getEndTime().isBefore(seckillPromotion.getStartTime())){
            return R.error("时间设置不合理");
        }
        int isOK = seckillPromotionDao.updateSeckillPromotion(seckillPromotion);
        if (isOK>0){
            return R.ok();
        }
        return R.error();
    }

    @Override
    public Object canPromotionSession() {
        // 获取所有活动
        LocalDate localDate = LocalDate.now().plusDays(1L);
        List<SeckillPromotionEntity> seckillPromotionEntities = seckillPromotionDao.getCanPromotionSession(localDate);
        // 获取所有可用场次
        List<SeckillSessionEntity> seckillSessionEntities = seckillSessionService.canSession();
        // 获取
        return null;
    }

    @Override
    public List<SeckillPromotionEntity> getPromotionList() {
        LocalDate nowDate = LocalDate.now();
        List<SeckillPromotionEntity> seckillPromotionList = seckillPromotionDao.getSeckillPromotionList(null, 0, nowDate);
        return seckillPromotionList;
    }
}